package classes.pack;

import java.util.Vector;
import park.pack.R;
import park.pack.SplashScreen;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.ScrollView;

public class Profiling
{
	private Activity act;
	private Button [] buttons;

	private Profile currentProfile;
	private Vector<Profile> profiles = new Vector<Profile>();	
	private int iCurrent = 0;
	private int iMaxProfiles = 5;
		
	private EditText tName;
	private Button   btnSave;
	private Button 	btnCancel;
	
	private Button 	btnBack;
	
	private Button btnStartService; 
	private Button btnStopService;
	private Button btnStartParking;
	private Button btnStopParking;
	
	private CheckBox chkMa;
	private CheckBox chkDi;
	private CheckBox chkWo;
	private CheckBox chkDo;
	private CheckBox chkVrij;
	private CheckBox chkZa;
	private CheckBox chkZo;
	private CheckBox chkCur;
	
	private EditText tRetryTime;
	
	private RadioButton bTestInterface;
	private RadioButton bSMS;	
	
	public Profiling(Activity a)
	{
		act = a;
		for(int i=0; i<iMaxProfiles; i++)
			profiles.add(i, new Profile());	
		
		fillProfiles();
		setCurrentProfile();
	}
	
	public void setView()
	{
		createMenu();
	}
	
	public void createMenu()
    {		
		//create new dynamic view
	    ScrollView sv = new ScrollView(act);
	    LinearLayout ll = new LinearLayout(act);
	    ll.setOrientation(LinearLayout.VERTICAL);
	    sv.addView(ll);	  
	    
	    buttons = new Button[iMaxProfiles];
	                     
	    for(int i = 0; i < iMaxProfiles; i++) 
	    {	            
	         buttons[i] = new Button(act);
	         buttons[i].setText(profiles.get(i).getName());
	         buttons[i].setId(i);
	         buttons[i].setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_profile, 0, 0, 0);
	         //buttons[i].setBackgroundResource(R.drawable.button);
	         ll.addView(buttons[i]);
	    }
	    
	    for(int i = 0; i < iMaxProfiles; i++) 
	    {	
	    	buttons[i].setOnClickListener(new View.OnClickListener() 
	        {		    	
	        	public void onClick(View v) 
	        	{          		
	        		Button vg =(Button)v;	        		
	        		int index = vg.getId();        		
	        		iCurrent = index;
	        		
	        		act.setContentView(R.layout.profiling);	        		
	        		                
	        		setViews(); 
	                setValues(index);	                
	                setListeners(); 	        		
	        	}
	        });
	    } 
	    
	    Button btnBack = new Button(act);

	    int backID = R.drawable.button;
	    
	    btnBack.setText("Terug naar menu");
	    btnBack.setId(backID);
	    btnBack.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_backred, 0, 0, 0);
	    
	    //btnBack.setBackgroundResource(R.drawable.button);
	    	    
	    ll.addView(btnBack);	    
	    
	    btnBack.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{ 
        		act.setContentView(R.layout.main);
        		
        		btnStartService = (Button)Gui.getUiMain().findViewById(R.id.btnStartService);
                btnStopService = (Button)Gui.getUiMain().findViewById(R.id.btnStopService);
                btnStartParking = (Button)Gui.getUiMain().findViewById(R.id.btnStartParking);
                btnStopParking = (Button)Gui.getUiMain().findViewById(R.id.btnStopParking);
        		
                btnStartParking.setVisibility(View.VISIBLE);//Visible
            	btnStopParking.setVisibility(View.GONE);//Gone
            	
        		btnStartService.setVisibility(View.VISIBLE);//Visible
                btnStopService.setVisibility(View.GONE);//Gone
        		
        		Gui.getUiMain().SetOnClickListeners();
        	}
        });
	    
	  //set view
	    act.setContentView(sv);   
    }

	
	public Profile setCurrentProfile()
	{		
		currentProfile = profiles.get(loadPreferencesInt("current", 0));
		return currentProfile;
	}	
	
	public Profile GetCurrentProfile()
	{
		fillProfiles();
		return this.currentProfile;
	}
	
	public void setListeners()
	{
		btnSave.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{          		
        		savePreferencesInt("retryTime" 	+ Integer.toString(iCurrent),	Integer.parseInt(tRetryTime.getText().toString()));
        		savePreferencesString("naam"	+ Integer.toString(iCurrent),	tName.getText().toString());	
        		savePreferencesBool("maandag" 	+ Integer.toString(iCurrent), 	chkMa.isChecked());
        		savePreferencesBool("dinsdag" 	+ Integer.toString(iCurrent), 	chkDi.isChecked());
        		savePreferencesBool("woensdag" 	+ Integer.toString(iCurrent),	chkWo.isChecked());
        		savePreferencesBool("donderdag" + Integer.toString(iCurrent),	chkDo.isChecked());
        		savePreferencesBool("vrijdag" 	+ Integer.toString(iCurrent),	chkVrij.isChecked());
        		savePreferencesBool("zaterdag" 	+ Integer.toString(iCurrent), 	chkZa.isChecked());
        		savePreferencesBool("zondag" 	+ Integer.toString(iCurrent), 	chkZo.isChecked()); 
        		if(chkCur.isChecked())
        			savePreferencesInt("current", iCurrent);
        		
        		fillProfiles();	
        		createMenu();
        	}
        });
		
		btnCancel.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{ 
        		createMenu();
        	}
        });
	}
	
	public void setViews()
	{
		chkMa 	= (CheckBox)act.findViewById(R.id.chkMa);
        chkDi 	= (CheckBox)act.findViewById(R.id.chkDi);
        chkWo 	= (CheckBox)act.findViewById(R.id.chkWo);
        chkDo 	= (CheckBox)act.findViewById(R.id.chkDo);
        chkVrij = (CheckBox)act.findViewById(R.id.chkVrij);
        chkZa	= (CheckBox)act.findViewById(R.id.chkZa);
        chkZo 	= (CheckBox)act.findViewById(R.id.chkZo);  
        chkCur	= (CheckBox)act.findViewById(R.id.chkCur);  
              
        btnSave 	= (Button)act.findViewById(R.id.btnSave);
        btnCancel	= (Button)act.findViewById(R.id.btnCancel);
        tName		= (EditText)act.findViewById(R.id.tName);
        tRetryTime 	= (EditText)act.findViewById(R.id.tRetryTime);
        
        btnSave.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_save, 0, 0, 0);
        btnCancel.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_stop, 0, 0, 0);
	}
	
	public void setValues(int index)
	{		
		Profile prof = profiles.get(index);
			
		chkMa.setChecked(prof.getMaandag());
	    chkDi.setChecked(prof.getDinsdag());
	    chkWo.setChecked(prof.getWoensdag());
	    chkDo.setChecked(prof.getDonderdag());
	    chkVrij.setChecked(prof.getVrijdag());
	    chkZa.setChecked(prof.getZaterdag());
	    chkZo.setChecked(prof.getZondag());
	    chkCur.setChecked(prof.getCurrent());
	    tName.setText(prof.getName());
	    tRetryTime.setText(Integer.toString(prof.getRetryTime()));
	    
	}
	
	public void fillProfiles()
	{
		for(int i = 0; i<iMaxProfiles; i++)
		{
			Profile prof = profiles.get(i);
			prof.setMaandag(loadPreferencesBool("maandag" + Integer.toString(i)));
	        prof.setDinsdag(loadPreferencesBool("dinsdag" + Integer.toString(i)));
	        prof.setWoensdag(loadPreferencesBool("woensdag" + Integer.toString(i)));
	        prof.setDonderdag(loadPreferencesBool("donderdag" + Integer.toString(i)));
	        prof.setVrijdag(loadPreferencesBool("vrijdag" + Integer.toString(i)));
	        prof.setZaterdag(loadPreferencesBool("zaterdag" + Integer.toString(i)));
	        prof.setZondag(loadPreferencesBool("zondag" + Integer.toString(i)));    
	        prof.setName(loadPreferencesString("naam" + Integer.toString(i)));
	        prof.setRetryTime(loadPreferencesInt("retryTime" + Integer.toString(i), 5));
	        
	        int index = loadPreferencesInt("current", 0);
	        if(index == i)
	        	prof.setCurrent(true);
	        else
	        	prof.setCurrent(false);
		}
	}
	
	public void savePreferencesString(String key, String value )
	{
		SharedPreferences sharedPreferences = act.getSharedPreferences("profile", Context.MODE_WORLD_READABLE);
	    SharedPreferences.Editor editor = sharedPreferences.edit();	    
	    editor.putString(key, value);
	    editor.commit();
	}
	
	public void savePreferencesBool(String key, boolean value)
	{
	    SharedPreferences sharedPreferences = act.getSharedPreferences("profile", Context.MODE_WORLD_READABLE);
	    SharedPreferences.Editor editor = sharedPreferences.edit();	    
	    editor.putBoolean(key, value);
	    editor.commit();
	}
	
	public void savePreferencesInt(String key, int value)
	{
		SharedPreferences sharedPreferences = act.getSharedPreferences("profile", Context.MODE_WORLD_READABLE);
	    SharedPreferences.Editor editor = sharedPreferences.edit();	    
	    editor.putInt(key, value);
	    editor.commit();
	}
	  
	public String loadPreferencesString(String strName)
	{
		SharedPreferences sharedPreferences = act.getSharedPreferences("profile", Context.MODE_WORLD_READABLE); 
	    String strPref = sharedPreferences.getString(strName, "Profiel");
	    
	    return strPref;
	}
	
	public boolean loadPreferencesBool(String strName)
	{
		SharedPreferences sharedPreferences = act.getSharedPreferences("profile", Context.MODE_WORLD_READABLE); //!!!!! getPreferences() is a void method, so this line will return a nullPointerException!!!!
	    boolean strPref = sharedPreferences.getBoolean(strName, true);
	    
	    return strPref;
		
	}
	
	public int loadPreferencesInt(String strName, int iStandard)
	{
		SharedPreferences sharedPreferences = act.getSharedPreferences("profile", act.MODE_WORLD_READABLE);
	    int strPref = sharedPreferences.getInt(strName, iStandard);
	    
	    return strPref;
	}
	
	/////////////////////
	////// GETTERS //////
	
	//Gets the preferred external interface on which the program must send data
	public ExtInterfaceType getPreferedExtinterface()
	{/*
		if(LoadPreferencesBool("SMS") == true) 
		{
			return ExtInterfaceType.SMS;
		}
		else
		{	
			return ExtInterfaceType.TEST;
		}
		*/
		return ExtInterfaceType.TEST;//to prevent crashing (temporary solution)
		
	}
	//Gets the preferred Method of tracking the users location
	public GeoLocType getPreferedGeoLocType()
	{
		return GeoLocType.GPS;
	}
	
	public int getPreferedTimeBetweenPosChecks()//in seconds
	{
		int testTime = 3;
		return testTime;
	}
	
	public int getRetryTime()
	{
		return loadPreferencesInt("sRetryTime", 5);
	}
	//////GETTERS //////
	/////////////////////

	
	/////////////////////
	//////SETTERS //////
		
	
	//Sets the preferred external interface on which the program must send data
	public void setPreferredExtInterface(String extInterface)
	{
		
	}
	//Sets the preferred Method of tracking the users location
	public void setPreferredGeoLoc(String geoLoc)
	{
		
	}
	
	//////SETTERS //////
	/////////////////////
	
	public void loadProfiles()
	{
		//load all profiles from xml
	}
	
	/*public boolean isMonday()
	{
		return currentProfile.getMaandag();
	}*/
}
